package dao;

import bean.StudentJob;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import util.Util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class JobDao {
    public static int getMaxId(){
        Connection conn =Util.getConn();
        String sql = "select id from job order by id desc";
        PreparedStatement pstmt;
        int id=0;
        try {
            pstmt = (PreparedStatement)conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                id=rs.getInt("id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return id;
    }
    public static int saveJobInfo(StudentJob studentJob) {
        Connection conn = Util.getConn();
        int i = 0;
        String sql = "insert into job(studentId,jobtime,gitAddress,jobNumber) values(?,?,?,?)";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setString(1, studentJob.getStudentId());

            System.out.println(studentJob.getJobDate());
            Timestamp t = new Timestamp(studentJob.getJobDate().getTime());
            pstmt.setTimestamp(2,t);
            pstmt.setString(3, studentJob.getJobGitAddress());
            pstmt.setInt(4,studentJob.getJobNumber());
            i = pstmt.executeUpdate();
            int id=getMaxId();
            System.out.println(id);
            GitVersionInfoDao.saveVersion(studentJob,id);
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
